package com.zima.mall.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class UploadUtils {
    //图片上传位置
    private static final String BASE_PATH = "http://123.56.89.192:8080/www/server/tomcat/webapps/upload/";

    //
    private static final String SERVER_PATH = "http://123.56.89.192:8080/upload/";


    public static String upload(MultipartFile file){
        String filename = file.getOriginalFilename();//获取图片名字

        String uuid = UUID.randomUUID().toString().replace("-","");//为保证图片在服务器中名字的唯一性，用uuid改写

        String newFilename = uuid + "-" + filename;//拼接新名字

        File image = new File(BASE_PATH,newFilename);

        try {
            file.transferTo(image);
        } catch (IOException e) {
           return null;
        }

        return SERVER_PATH + newFilename;

    }
}
